package ua.epam.muzalevskiy.traction.commands;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.epam.muzalevskiy.traction.configuretion.MessageConfigurer;
import ua.epam.muzalevskiy.traction.configuretion.PagePathConfigurer;
import ua.epam.muzalevskiy.traction.configuretion.PageViewConfigurer;
import ua.epam.muzalevskiy.traction.dao.DaoFactory;
import ua.epam.muzalevskiy.traction.dao.VehicleDao;
import ua.epam.muzalevskiy.traction.dao.exceptions.NotUniqueParameterException;
import ua.epam.muzalevskiy.traction.vehicle.Vehicle;
import ua.epam.muzalevskiy.traction.vehicle.VehicleFactory;
import ua.epam.muzalevskiy.traction.vehicle.VehicleType;

/**
 * <code>CreateVehicleCommand</code> class is the <code>Command</code> 
 * interface implementation, that realized <code>Command</code> pattern.
 * Current class encapsulate action of adding new information about vehicle
 * to data base. 
 *
 * @author Muzalevskiy Vitaliy
 * @see Command
 * @see Vehicle
 * @see VehicleDao
 */
public class CreateVehicleCommand implements Command {

    private static DaoFactory factory = new DaoFactory();
    private VehicleDao vehicleDao = factory.getVehicleDao();
    private static VehicleFactory vehicleFactory = new VehicleFactory();
    private static final String PARAM_NAME_ID = "id";
    private static final String PARAM_NAME_NUMBER = "number";
    private static final String PARAM_NAME_TYPE = "type";

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page;
        try {
            int id = Integer.valueOf(request.getParameter(PARAM_NAME_ID));
            String number = request.getParameter(PARAM_NAME_NUMBER);
            String type = request.getParameter(PARAM_NAME_TYPE);
            Vehicle vehicle = vehicleFactory.createVehicle(id, number, 
                    getVehicleType(type));
            vehicleDao.addVehicle(vehicle);
            page = PagePathConfigurer.getInstance()
                    .getProperty(PagePathConfigurer.ADMINISTRATION_PAGE);
        } catch (NotUniqueParameterException ex) {
            request.setAttribute(Command.ATTRIBUTE_NAME_ERROR_MESSAGE, 
                    MessageConfigurer.getInstance()
                    .getProperty(MessageConfigurer.ADD_NEW_VEHICLE_ERROR_MESSAGE));
            page = PagePathConfigurer.getInstance()
                    .getProperty(PagePathConfigurer.ERROR_PAGE);
        }
        return page;
    }

    private VehicleType getVehicleType(String type) {
        VehicleType vehicleType = null;
        if (PageViewConfigurer.getInstance()
                .getProperty(PageViewConfigurer.BUS).equals(type)) {
            vehicleType = VehicleType.BUS;
        } else if (PageViewConfigurer.getInstance()
                .getProperty(PageViewConfigurer.TROLLEYBUS).equals(type)) {
            vehicleType = VehicleType.TROLLEYBUS;
        } else if (PageViewConfigurer.getInstance()
                .getProperty(PageViewConfigurer.TRAM).equals(type)) {
            vehicleType = VehicleType.TRAM;
        } else {
            throw new RuntimeException();
        }
        return vehicleType;
    }
}
